$(function () {
    let thisID = 0;
    /*----------------------------------------------------*/
    initPage();

    $('.parentTypeList').on('click', 'a' ,function () {
        let that = $(this);
        $.ajax({
            url: '/manage/racetype/get_raceType.do',
            type: 'post',
            data: {
                parentId : that.attr('data-id')
            },
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function (data) {
                if (data.status == 0) {
                    To_sonItemBar( data.data, ()=>{
                        that.parents('li').addClass('active').siblings().removeClass('active');
                        $('#addItemType').attr('data-partenID',that.attr('data-id'));
                    });
                }else if( data.status == 10 ){
                    spop_waring( data.msg, function () {}, outLogin );
                    return false;
                }else {
                    spop_er( data.msg );
                }
            }
        });
    });

    $('#newRaceType-model').on('show.bs.modal',function () {
        $('#racetypeName').val('');
    });
    $('#newRaceTypeModel-btn').on('click',function () {
        let racetypeName = $('#racetypeName').val().trim();
        if( !racetypeName ){
            spop_waring( '输入不能为空' );
            return false;
        }
        addNewRaceType( racetypeName );
    });

    $('#newItem-model').on('show.bs.modal',function () {
        $('#racetype').val('');
        $('#startTime').val('');
        $('#stopTime').val('');
    });
    $('#itemTypeModel-btn').on('click',function (e) {
        let itemTypeName = $('#racetype').val();
        let startTime = $('#startTime').val();
        let stopTime = $('#stopTime').val();
        let thisParentId = $('#addItemType').attr('data-partenID');
        if( !itemTypeName || !startTime || !stopTime ){
            spop_waring( '输入不能为空' );
            return false;
        }
        addNewItemType( thisParentId, itemTypeName, startTime, stopTime );
    });

    $('#changeRaceType-model').on('show.bs.modal', (e)=>{
        thisID = $(e.relatedTarget).parent().prev().attr('data-id');
        let namn = $(e.relatedTarget).parent().prev().text();
        $('#changeRaceType-model').find('.form-control-static').text( namn );
        $('#newParentName').val('');
    });
    $('#changeParentType-btn').on('click', ()=>{
        let newName = $('#newParentName').val().trim();
        if( newName ){
            $.ajax({
                url: '/manage/racetype/set_racetype_name.do',
                type: 'post',
                data: {
                    racetype : newName,
                    racetypeno: thisID
                },
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    if (data.status == 0) {
                        spop_ok(data.msg,()=>{
                            initPage();
                            $('#changeRaceType-model').modal('hide');
                        });
                    } else if(data.status == 10){
                        spop_waring(data.msg,()=>{},outLogin);
                        return false;
                    }else{
                        spop_er(data.msg);
                    }
                }
            });
        }else {
            spop_waring('输入不能为空');
        }
    });

    $('#changeItem-model').on('show.bs.modal', (e)=>{
        thisID = $(e.relatedTarget).parent().prev().attr('data-id');
        let namn = $(e.relatedTarget).parent().prev().text();
        $('#changeItem-model').find('.form-control-static').text( namn );
        $('#newItemName').val('');
    });
    $('#changeItemName-btn').on('click', ()=>{
        console.log('zzzz');
        let newName = $('#newItemName').val().trim();
        if( newName ){
            $.ajax({
                url: '/manage/racetype/set_racetype_name.do',
                type: 'post',
                data: {
                    racetype : newName,
                    racetypeno: thisID
                },
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    if (data.status == 0) {
                        spop_ok(data.msg,()=>{
                            initPage();
                            $('#changeItem-model').modal('hide');
                        });
                    } else if(data.status == 10){
                        spop_waring(data.msg,()=>{},outLogin);
                        return false;
                    }else{
                        spop_er(data.msg);
                    }
                }
            });
        }else {
            spop_waring('输入不能为空');
        }
    });

    $('.sonTypeList').on('click','i.glyphicon-trash',(e)=>{
        thisID = $(e.currentTarget).parent().prev().attr('data-id');
        let thisName = $(e.currentTarget).parent().prev().text();
        if( confirm('确认删除<<'+ thisName +'>>') ){
            delTypeOrItem(thisID);
        }
    });
    $('.parentTypeList').on('click','i.glyphicon-trash',(e)=>{
        thisID = $(e.currentTarget).parent().prev().attr('data-id');
        let thisName = $(e.currentTarget).parent().prev().text();
        if( confirm('确认删除<<'+ thisName +'>>') ){
            delTypeOrItem(thisID);
        }
    });

    function initPage() {
        let Pr_parentSele = new Promise( (resolve,reject)=> {
            $.ajax({
                url: '/manage/racetype/get_raceType.do',
                type: 'post',
                data: {parentId: 0},
                dataType: 'json',
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    if (data.status == 0) {
                        resolve(data);
                    } else {
                        reject(data);
                        return false;
                    }
                }
            });
        });
        Pr_parentSele.then( (data)=>{
            To_parentType( data.data , function () {
                $('.parentTypeList').find('a').eq(0).click();
            } );
        }).catch( (data)=>{
            if( data.status == 10 ){
                spop_waring( data.msg, ()=>{}, outLogin );
            }else{
                spop_er( data.msg );
            }
        } )
    } // --ok

    function To_parentType( data ,endFn ) {
        let Box = $('.parentTypeList');
        Box.html('');

        $(data).each( (idx, item)=>{
            console.log(item);
            let DOM = $('<li class="list-group-item">' +
                '           <a data-id="'+ item.racetypeno +'" href="javascript:;">'+ item.racetype +'</a>' +
                '           <div class="Po-a">' +
                '              <i class="glyphicon glyphicon-pencil" data-toggle="modal" data-target="#changeRaceType-model" data-whatever="@mdo"></i>' +
                '              <i class="glyphicon glyphicon-trash"></i>' +
                '           </div>' +
                '        </li>');
            Box.append( DOM );
        });
        if( typeof endFn === 'function'){
            endFn();
        }
    }

    function To_sonItemBar( data, endFn ) {
        let Box = $('.sonTypeList');
        Box.html('');
        $(data).each( (idx, item)=>{
            let DOM = $('<li class="list-group-item ">' +
                '           <a data-id="'+ item.racetypeno +'" href="./itemType-info.html?racetypeno='+ item.racetypeno +'">'+ item.racetype +'</a>' +
                '           <div class="Po-a">' +
                '               <i class="glyphicon glyphicon-pencil" data-toggle="modal" data-target="#changeItem-model"></i>' +
                '               <i class="glyphicon glyphicon-trash"></i>' +
                '           </div>' +
                '        </li>');
            Box.append( DOM );
        });

        if( typeof endFn === 'function'){
            endFn();
        }
    }

    function addNewRaceType( name ) {
        let Pr_newType = new Promise( (resolve,reject)=>{
            $.ajax({
                url: '/manage/racetype/add_racetype.do',
                type: 'post',
                data: {
                    parentId : 0,
                    racetype : name
                },
                dataType: 'json',
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    if (data.status == 0) {
                        resolve(data);
                    } else {
                        reject(data);
                        return false;
                    }
                }
            });
        });
        Pr_newType.then((data)=>{
            spop_ok(data.msg, ()=>{
                initPage();
                $('#newRaceType-model').modal('hide');
            } )
        }).catch((data)=>{
            spop_er(data.msg);
        });
    }

    function addNewItemType( thisID, name, startTime ,stopTime ) {
        let Pr_newItem = new Promise( (resolve,reject)=>{
            $.ajax({
                url: '/manage/racetype/add_racetype.do',
                type: 'post',
                data: {
                    parentId : thisID,
                    racetype : name,
                    startTime: startTime,
                    stopTime: stopTime
                },
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    if (data.status == 0) {
                        resolve(data);
                    } else {
                        reject(data);
                        return false;
                    }
                }
            });
        });
        Pr_newItem.then((data)=>{
            spop_ok(data.msg, ()=>{
                initPage();
                $('#newItem-model').modal('hide');
            } )
        }).catch((data)=>{
            spop_er(data.msg);
        });
    }

    function delTypeOrItem( thisID ) {
        $.ajax({
            url: '/manage/racetype/delete_racetype.do',
            type: 'post',
            data: {
                racetypeno: thisID
            },
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function (data) {
                if (data.status == 0) {
                    initPage();
                } else if(data.status == 10 ){
                    spop_waring(data.msg, ()=>{}, outLogin );
                }else{
                    spop_waring( data.msg);
                }
            }
        });
    }

});